package com.jm.smsgateway.config;

import com.jm.common.constant.RabbitMQConstants;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 针对性的配置，可以才有当前方式，如果通用的直接可以使用yml的配置
 * @author JianWu
 */
@Configuration
public class RabbitMQConfig {


    @Bean
    public Exchange normalExchange(){
        return ExchangeBuilder.fanoutExchange(RabbitMQConstants.SMS_GATEWAY_NORMAL_EXCHANGE).durable(true).build();
    }

    @Bean
    public Queue normalQueue(){
        return QueueBuilder.durable(RabbitMQConstants.SMS_GATEWAY_NORMAL_QUEUE)
                .withArgument("x-message-ttl",10000)
                .withArgument("x-dead-letter-exchange",RabbitMQConstants.SMS_GATEWAY_DEAD_EXCHANGE)
                .withArgument("x-dead-letter-routing-key","")
                .build();
    }

    @Bean
    public Binding normalBinding(Exchange normalExchange,Queue normalQueue){
        return BindingBuilder.bind(normalQueue).to(normalExchange).with("").noargs();
    }

    @Bean
    public Exchange deadExchange(){
        return ExchangeBuilder.fanoutExchange(RabbitMQConstants.SMS_GATEWAY_DEAD_EXCHANGE).durable(true).build();
    }

    @Bean
    public Queue deadQueue(){
        return QueueBuilder.durable(RabbitMQConstants.SMS_GATEWAY_DEAD_QUEUE).build();
    }

    @Bean
    public Binding deadBinding(Exchange deadExchange,Queue deadQueue){
        return BindingBuilder.bind(deadQueue).to(deadExchange).with("").noargs();
    }

//    @Bean
//    public SimpleRabbitListenerContainerFactory gatewayContainerFactory(ConnectionFactory connectionFactory,
//                                                                        SimpleRabbitListenerContainerFactoryConfigurer configurer){
//        SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory = new SimpleRabbitListenerContainerFactory();
//        simpleRabbitListenerContainerFactory.setConcurrentConsumers(5);
//        simpleRabbitListenerContainerFactory.setPrefetchCount(10);
//        simpleRabbitListenerContainerFactory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
//        configurer.configure(simpleRabbitListenerContainerFactory,connectionFactory);
//        return simpleRabbitListenerContainerFactory;
//    }

}